package com.cskaoyan.controller;

import com.cskaoyan.bean.BO.WxCouponBo;
import com.cskaoyan.bean.VO.WxCouponVo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.WxCouponService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName WxCouponController
 * @Description TODO
 * @Author ke
 * @Date 2022/8/18 9:17
 * @Version 1.0
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    WxCouponService wxCouponService;


    // 0未使用 1已使用 2已过期
    @RequestMapping("mylist")
    public BaseRespVo mylist(Integer status, Integer page, Integer limit) {
        CommonData<WxCouponVo> data = wxCouponService.myList(status, page, limit);
        return BaseRespVo.ok(0, data, "成功");
    }

    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody WxCouponBo bo) {

        int code = wxCouponService.exchange(bo);
        return BaseRespVo.ok(0, null, "成功");
    }

    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info) {
        CommonData data = wxCouponService.List(info);
        return BaseRespVo.ok(0, data, "成功");
    }

    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody String couponId) {
        Session session = SecurityUtils.getSubject().getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return BaseRespVo.ok(740, null, "请重新登入！");
        }
        int cId = 0;
        if (couponId != null && !"".equals(couponId)) {
            cId = Integer.parseInt(couponId);
        }
        int code = wxCouponService.receiveById(cId, userId);
        if (code == 2) {
            return BaseRespVo.ok(740, null, "优惠券已领取过");
        } else if (code == 1) {
            return BaseRespVo.ok(740, null, "领取失败");
        }
        return BaseRespVo.ok(0, null, "成功");
    }


}
